﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tabwin
{
    public partial class Index : Form
    {
        public Index()
        {
            InitializeComponent();
        }

        private void Index_Load(object sender, EventArgs e)
        {
            this.InitTreeView();
        }

        //初始化TreeView
        private void InitTreeView()
        {
            this.tree.Nodes.Clear();
            TreeNode root = new TreeNode("管理信息系统", 0, 1);
            this.InitializeNavigationGuest(root);
            this.tree.Nodes.Add(root);
            root.Expand();
        }

        /// <summary>
        /// Guest用户初始化导航。
        /// </summary>
        private void InitializeNavigationGuest(TreeNode root)
        {
            TreeNode node = new TreeNode("相关连接", 0, 1);
            TreeNode itemNode = new TreeNode("登录", 2, 2);
            itemNode.Tag = "login";
            node.Nodes.Add(itemNode);
            itemNode = new TreeNode("关于", 2, 2);
            itemNode.Tag = "about";
            node.Nodes.Add(itemNode);

            root.Nodes.Add(node);
        }

        private void tree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                if (e.Node.Tag.ToString() == "login")
                {
                    MoudelCommon.moudelName = e.Node.Tag.ToString();
                    if (!hadMoudel("login"))
                    {
                        this.LoadFormMoudel("login");
                    }
                }

                if (e.Node.Tag.ToString() == "about")
                {
                    MoudelCommon.moudelName = e.Node.Tag.ToString();
                    if (!hadMoudel("about"))
                    {
                        this.LoadFormMoudel("about");
                    }

                }

            }
        }

        public void LoadFormMoudel(string name)
        {
            Loader f = new Loader();
            f.MdiParent = this.ParentForm;
            f.Dock = DockStyle.Fill;
            f.Text = name;
            f.Show();
            MoudelCommon.moudelList.Add(name);
        }


        public bool hadMoudel(string name)
        {
            bool had = false;
            var p = from item in MoudelCommon.moudelList
                    where item == name
                    select item;
            int count = p.ToList().Count;
            if (count > 0)
                had = true;
            else
                had = false;
            count = 0;
            return had;
        }

    }
}
